package com.briup.smartcity_group7.web.controller;

import com.briup.smartcity_group7.bean.basic.BaseUser;
import com.briup.smartcity_group7.service.IBaseUserService;
import com.briup.smartcity_group7.util.Result;
import com.briup.smartcity_group7.util.ResultUtil;
import com.briup.smartcity_group7.web.vo.RegisterUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: lenovo
 * @Date: 2023/01/04/18:12
 * 登录相关控制模块
 */
@Api(tags = "登录相关模块")
@RestController
public class LoginController {
    @Autowired
    private IBaseUserService service;
    @ApiOperation("用户登录")
    @ApiImplicitParams({    //接收参数
            @ApiImplicitParam(name = "username",value = "用户名",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true)
    })
    @PostMapping(value = "/login",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result login(String username, String password){
        //1.调用service层方法接口--》具体实现,传递参数-->业务逻辑判断操作
        String token = service.login(username, password);
        return ResultUtil.success(token);
    }
    //注册操作--》添加描述接口和参数的文字信息
    @ApiOperation("用户注册")
    @PostMapping(value = "/register",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result register(RegisterUserVO vo){
        //1、将接收到的参数信息传递给service层
        BaseUser baseUser = new BaseUser();
        //vo对象向baseUser对象转化过程：vo视图对象复制成service业务对象
        BeanUtils.copyProperties(vo,baseUser);
        service.addUser(baseUser);
        return ResultUtil.success("注册成功，请登录");
    }
    //登出操作--》添加描述接口和参数的文字信息
    @ApiOperation("用户退出登录")
    @PostMapping("/logout")
    public Result logout(){
        /*
        退出登录的功能，目前只实现返回正确的字符串格式
        扩展再扩展：用户信息保存效果（登录后将用户信息缓存，登出后将缓存信息进行清楚）
                  修改登录后的是否在线状态--》App
         */
        return ResultUtil.success("退出成功");
    }
}
